package com.example.pictureManage.controller;

import com.example.pictureManage.service.imageDetailService.imagePlayer;
import com.example.pictureManage.service.imageDetailService.imageZoom;
import com.example.pictureManage.util.FileUtil;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;

import java.net.URI;
import java.nio.file.Paths;

/***
 * @author 郑伯洋
 * 用于查看图片的窗口业务
 */
public class ImageDetailController {
    @FXML
    private ImageView detailImageView;

    private Stage stage;

    // 记录拖拽前图片初始的偏移量
    private Point2D translateOffset = null;

    // 记录开始拖拽前的鼠标位置
    private Point2D preMousePosition = null;

    // 图片缩放器
    private final imageZoom zoomControl = new imageZoom();

    //图片播放器  向前、向后
    private final imagePlayer player = new imagePlayer();


    // 初始化图片和窗口
    public void initialize(Image image, Stage stage) {
        this.detailImageView.setImage(image);
        this.stage = stage;
//        this.stage.getIcons().add(new Image("D:\\Project\\ElectronicPictureManagementProgram\\src\\main\\java\\com\\example\\pictureManage\\controller\\icons\\icons8-picture-16.png"));

        //其实这里不应该写这么多，但是觉得这么点东西分层也不太好意思
        detailImageView.addEventHandler(ScrollEvent.SCROLL,e->{
            //向上滚轮
            if (e.getDeltaY() > 0){
                zoomControl.handleUp(detailImageView);
            }
            //向下滚轮
            else{
                zoomControl.handleDown(detailImageView);
            }
        });
        //这里是拖拽业务    拖拽前点击
        detailImageView.addEventHandler(MouseEvent.MOUSE_PRESSED,e->{
            //鼠标位置
            preMousePosition = new Point2D(e.getSceneX(),e.getSceneY());
            translateOffset = new Point2D(detailImageView.getTranslateX(),detailImageView.getTranslateY());

        });
        //拖拽时
        detailImageView.addEventHandler(MouseEvent.MOUSE_DRAGGED,e->{
            // 计算鼠标移动的偏移量
            Point2D currentMousePosition = new Point2D(e.getSceneX(), e.getSceneY());
            Point2D offset = currentMousePosition.subtract(preMousePosition);

            // 更新图片的位置
            detailImageView.setTranslateX(translateOffset.getX() + offset.getX());
            detailImageView.setTranslateY(translateOffset.getY() + offset.getY());
        });

        stage.setOnCloseRequest(e->{
            //关闭定时任务   如果有的话
            if (player.getAnimationTimer() != null) {
                player.getAnimationTimer().stop();
            }
        });
    }

    // 关闭窗口
    @FXML
    private void handleClose() {
        stage.close();
        if (player.getAnimationTimer() != null) {
            player.getAnimationTimer().stop();
        }
    }

    // 图片 旋转的功能函数
    @FXML
    private void handleRotate() {
        if (detailImageView != null) {
            detailImageView.setRotate(detailImageView.getRotate() + 90);
        }
    }

    //切换到下一张图片
    @FXML
    private void handleNext(){
        player.imageNext(detailImageView);
        detailImageView.setTranslateX(0);
        detailImageView.setTranslateY(0);
        //获取到当前图片的名称
        String url = detailImageView.getImage().getUrl();
        String fileName = FileUtil.getFileName(Paths.get(URI.create(url)));
        //设置名称
        stage.setTitle(fileName);
    }

    //切换到上一张图片
    @FXML
    private void handlePre(){
        player.imagePre(detailImageView);

        //获取到当前图片的名称
        String url = detailImageView.getImage().getUrl();
        String fileName = FileUtil.getFileName(Paths.get(URI.create(url)));
        //设置名称
        stage.setTitle(fileName);
    }

    //播放幻灯片
    @FXML
    public void handlePlay() {
        stage.setTitle("播放中");
        player.imagePlay(detailImageView);
    }

    //停止幻灯片
    @FXML
    private void handleStop(){
        player.imageStop();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(null);
        alert.setHeaderText(null);
        alert.setContentText("已经停止播放  Ciallo～(∠・ω< )⌒★");
        alert.show();

        //获取到当前图片的名称
        String url = detailImageView.getImage().getUrl();
        String fileName = FileUtil.getFileName(Paths.get(URI.create(url)));
        //设置名称
        stage.setTitle(fileName);
    }
    //放大
    @FXML
    private void handleUp(){
        zoomControl.handleUp(detailImageView);
    }

    //缩小
    @FXML
    private void handleDown(){
        zoomControl.handleDown(detailImageView);
    }

}